﻿#region Using
using System.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Sub.Function;
using System.Collections;
using System.Web.Security;

#endregion

namespace Extention.Control
{
    public partial class usrRibbon : UserControl
    {
       public delegate void OnTabSelected(object sender, RibbonBarPage Page, string ModuleName);
        public delegate void OnMenuItemClick(object sender, RibbonBarButtonItem item, MenuInfo Info);
        public delegate void OnNeedMoreItemsClick(object sender, ref object DataSource, string Key);
        public delegate void OnLanguageChanged(object sender, string LanguageCode);
        public event OnMenuItemClick MenuItemClick;
        public event EventHandler ChangePassword;
        public event OnNeedMoreItemsClick NeedMoreItems;
        public event OnTabSelected TabSelected;
        public event EventHandler HomePageClick;
        public usrRibbon()
        {
            InitializeComponent();
            //this.ribbonBar1.SelectedIndexChanged += new EventHandler(ribbonBar1_SelectedIndexChanged);
            this.ribbonBar1.Click += new EventHandler(ribbonBar1_Click);
            this.ribbonBar1.AdvancedClicked += new EventHandler<RibbonBarGroupArgs>(ribbonBar1_AdvancedClicked);
        }
        void ribbonBar1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var item = ((List<MapPage>)this.ribbonBar1.Tag).FirstOrDefault(p => p.Page == ribbonBar1.Pages[ribbonBar1.SelectedIndex]);
            if (item == null)
            {
                return;
            }
            if (TabSelected != null)
            {
                TabSelected.Invoke(sender, item.Page, item.ModleName);
            }
        }
        void ribbonBar1_Click(object sender, EventArgs e)
        {
            //this.ribbonBar1.Pages[this.ribbonBar1.SelectedIndex]  
        }
        public event EventHandler LogoutItemSelected;
        public event OnLanguageChanged LanguageChanged;
        private void ucRibbon_Load(object sender, EventArgs e)
        {

        }
        Hashtable ListOfGroupData = null;
        public void InitGroupsOn_Ribbon()
        {
            this.ribbonBar1.Pages.Clear();
            ListOfGroupData = new Hashtable();
            var tabs = HRP.Provider.ModuleManager.Module.GetRegisteredModule(Membership.GetUser().ProviderUserKey).OrderBy(p => p.ModuleID);
            foreach (var tab in tabs)
            {
                var rbPage = new RibbonBarPage();

                rbPage.Text = tab.Caption; 
                if (tab.HomeMenu != null)
                {
                    var btn_Home = new RibbonBarButtonItem();
                    btn_Home.Tag = tab.HomeMenu;
                    btn_Home.Image = new Gizmox.WebGUI.Common.Resources.ImageResourceHandle(tab.HomeMenu.Image);
                    btn_Home.Text = tab.HomeMenu.Caption;
                    btn_Home.Context().MenuInfo = tab.HomeMenu;
                    btn_Home.OnClick(menu =>
                    {
                        frmMain frm = Application.OpenForms[0] as frmMain;
                                frm.WorkSpaces.OpenView(menu.Context().MenuInfo.Path, menu.Context().MenuInfo.Caption, menu.Context().MenuInfo.Image, menu.Context().MenuInfo.PluginType);

                        
                    });
                    var rb_Group_home = new RibbonBarGroup();
                    rbPage.Groups.Add(rb_Group_home);
                    rb_Group_home.Items.Add(btn_Home);

                }
                this.ribbonBar1.Pages.Add(rbPage);
                if (tab.Groups != null)
                {


                    foreach (var group in tab.Groups)
                    {
                        var rb_Group = new RibbonBarGroup();
                        rb_Group.Text = group.Caption;
                        rb_Group.Context().ModuleName = tab.Module;
                        rb_Group.Context().ResourceKey = group.Caption;
                        rb_Group.Context().GroupName = group.GroupName;
                        rb_Group.Control.Width = 350;
                        rb_Group.Context().ModuleResourceKey = tab.Caption;
                        if (!ListOfGroupData.Contains(rb_Group))
                        {
                            ListOfGroupData.Add(rb_Group, new List<MenuInfo>());
                        }
                        rb_Group.Text =  group.Caption;
                        rbPage.Groups.Add(rb_Group);


                        if (string.IsNullOrEmpty( rb_Group.Control.Parent.Parent.Context().ResourceKey ))
                        {

                            rb_Group.Control.Parent.Parent.Context().InitEditResourceContextMenu(this.ribbonBar1, "EditRibbonBar", tab.Module, tab.Caption, "Thay đổi nội dung ngôn ngữ của Tab đang kích hoạt");
                        }
                        var leftST = new RibbonBarStackItem();
                        var rightST = new RibbonBarStackItem();
                        leftST.Control.Width = 170;
                        rightST.Control.Width = 170;
                        rb_Group.Items.Add(leftST);
                        rb_Group.Items.Add(rightST);
                        int count = (group.Menus.Count < 6) ? group.Menus.Count : 6;
                        var displayMenus = group.Menus.OrderBy(p => p.Ord).ToList();
                        rb_Group.HasAdvanced = group.Menus.Count > 6;                    
                        for (int i = 0; i < count; i++)
                        {
                            var mnItem = new RibbonBarButtonItem()
                            {
                                Text =  group.Menus[i].Caption,
                                Image = new Gizmox.WebGUI.Common.Resources.ImageResourceHandle(group.Menus[i].Image)
                            };
                            ((List<MenuInfo>)ListOfGroupData[rb_Group]).Add(new MenuInfo()
                            {
                                Caption =  group.Menus[i].Caption,
                                Path = "~/" + tab.Module + "/" + group.Menus[i].Path,
                                ResourceKey = group.Menus[i].Caption,
                                ModuleName = group.Menus[i].ModuleName 
                            });
                            var stItem = rb_Group.Items[i % 2] as RibbonBarStackItem;
                            stItem.Items.Add(mnItem);
                            mnItem.Context().MenuInfo = new MenuInfo()
                                {
                                    Caption =  group.Menus[i].Caption,
                                    Path = "~/" + tab.Module + "/" + group.Menus[i].Path,
                                    ResourceKey=group.Menus[i].Caption
                                };
                            mnItem.Context().ModuleName = tab.Module; 
                            mnItem.OnClick(menu =>
                            {

                                frmMain frm = Application.OpenForms[0] as frmMain;
                                frm.WorkSpaces.OpenView(menu.Context().MenuInfo.Path, menu.Context().MenuInfo.Caption, menu.Context().MenuInfo.Image, menu.Context().MenuInfo.PluginType);
                            });
                        }
                    }
                }

            }
        }     
        void ribbonBar1_AdvancedClicked(object sender, RibbonBarGroupArgs e)
        {
            object[] tagInfos = e.Group.Tag as object[];             
            var data = HRP.Provider.ModuleManager.Module.GetListOfMenuInModuleByUser(new HRP.Provider.ModuleInfo()
            {
                Module = e.Group.Context().ModuleName,
                Path = "~/" + e.Group.Context().ModuleName
            }, e.Group.Context().GroupName, Membership.GetUser().ProviderUserKey);
            // NeedMoreItems.Invoke(this, ref data, e.Group.Tag.ToString());
            FormShowMoreItems.Text = e.Group.Text;
            var listOfMenus = this.ListOfGroupData[e.Group] as List<MenuInfo>;
            //listOfMenus.ForEach(item =>
            //{
            //    item.Caption = HRP.Provider.LanguageManager.Res(this, item.ModuleName, item.ResourceKey);
            //});
            FormShowMoreItems.LoadData(data, listOfMenus, e.Group.Context().GroupName, e.Group);
            FormShowMoreItems.Show();        }
        frmListOfMoreMenus _FormShowMoreItems;

        public frmListOfMoreMenus FormShowMoreItems
        {
            get
            {
                if (_FormShowMoreItems == null)
                {
                    _FormShowMoreItems = new frmListOfMoreMenus();
                    _FormShowMoreItems.FormClosed += new Gizmox.WebGUI.Forms.Form.FormClosedEventHandler(_FormShowMoreItems_FormClosed);
                    _FormShowMoreItems.ItemClick += new OnMenuItemClick(_FormShowMoreItems_ItemClick);
                }
                return _FormShowMoreItems;
            }
        }

        void _FormShowMoreItems_ItemClick(object sender, RibbonBarButtonItem item, MenuInfo Info)
        {
            if (this.MenuItemClick != null)
            {
                this.MenuItemClick.Invoke(this, item, Info);
                FormShowMoreItems.Close();
            }
        }

        void _FormShowMoreItems_FormClosed(object sender, FormClosedEventArgs e)
        {
            _FormShowMoreItems.Dispose();
            _FormShowMoreItems = null;
        }        

        void linkHomwePage_Click(object sender, EventArgs e)
        {
            if (HomePageClick != null)
            {
                HomePageClick.Invoke(this, EventArgs.Empty);
            }
        }

        void changePassword_Click(object sender, EventArgs e)
        {
            if (ChangePassword != null)
            {
                ChangePassword.Invoke(sender, e);
            }
        }

        void LanguageItem_Click(object sender, EventArgs e)
        {

            if (LanguageChanged != null)
            {
                LanguageChanged.Invoke(sender, ((MenuItem)sender).Tag.ToString());
            }
        }       

        void objRibbonBarButtonItem_Click(object sender, EventArgs e)
        {
            if (this.MenuItemClick != null)
            {


                this.MenuItemClick.Invoke(this, ((RibbonBarButtonItem)(sender)), ((MenuInfo)((RibbonBarButtonItem)(sender)).Tag));
            }
        }

        List<clsLoadedForm> _ListOfChildForms = null;
        public class clsLoadedForm
        {
            public string ID;
            public ContainerControl form;
        }
        public List<clsLoadedForm> ListOfChildForms
        {
            get
            {
                if (_ListOfChildForms == null)
                {
                    _ListOfChildForms = new List<clsLoadedForm>();
                }
                return _ListOfChildForms;
            }
        }
        public void Clear()
        {
            this.ribbonBar1.Pages.Clear();
        }
        class MapPage
        {
            public RibbonBarPage Page;
            public string ModleName;
        }
        public bool IsInit { get; set; }
    }
}